﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TeamPrasioliteWarGame.Models;

namespace TeamPrasioliteWarGame.Admin
{
    public partial class ManageUnitTypes : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        // The return type can be changed to IEnumerable, however to support
        // paging and sorting, the following parameters must be added:
        //     int maximumRows
        //     int startRowIndex
        //     out int totalRowCount
        //     string sortByExpression
        public IQueryable GridViewAllUnitTypes_GetData()
        {
            Entities db = new Entities();

            return db.GameUnits.OrderBy(u => u.Attack);
        }

        // The id parameter name should match the DataKeyNames value set on the control
        public void GridViewAllUnitTypes_UpdateItem(int id)
        {
            Entities db = new Entities();
            GameUnit item = db.GameUnits.SingleOrDefault(u => u.Id == id);
            // Load the item here, e.g. item = MyDataLayer.Find(id);
            if (item == null)
            {
                // The item wasn't found
                ModelState.AddModelError("", String.Format("Item with id {0} was not found", id));
                return;
            }
            TryUpdateModel(item);
            if (ModelState.IsValid)
            {
                // Save changes here, e.g. MyDataLayer.SaveChanges();
                db.SaveChanges();
            }
        }

        // The id parameter name should match the DataKeyNames value set on the control
        public void GridViewAllUnitTypes_DeleteItem(int id)
        {
            Entities db = new Entities();
            var unitToRemove = db.GameUnits.Include("UserUnits").SingleOrDefault(u => u.Id == id);
            var unitRelationsToRemove = unitToRemove.UserUnits.ToList();
            for (int i = 0; i < unitRelationsToRemove.Count; i++)
            {
                db.UserUnits.Remove(unitRelationsToRemove[i]);
            }
            db.GameUnits.Remove(unitToRemove);
            db.SaveChanges();
        }

        protected void ShowInsert_Click(object sender, EventArgs e)
        {
            this.ShowInsert.Visible = false;
            this.InsertPanel.Visible = true;
        }

        protected void CancelButton_Command(object sender, CommandEventArgs e)
        {
            this.ShowInsert.Visible = true;
            this.InsertPanel.Visible = false;
        }

        protected void CreateButton_Command(object sender, CommandEventArgs e)
        {
            Entities db = new Entities();
            using (db)
            {
                var newGameUnit = new GameUnit
                {
                    Type = this.NewUnitType.Text,
                    Attack = int.Parse(this.NewUnitAttack.Text),
                    Defense = int.Parse(this.NewUnitDefence.Text),
                    FoodCost = int.Parse(this.NewUnitFoodCost.Text),
                    IronCost = int.Parse(this.NewUnitIronCost.Text),
                    HP = int.Parse(this.newUnitHP.Text),
                    BuildTimeInSeconds = int.Parse(this.NewUnitBuildTime.Text)
                };

                db.GameUnits.Add(newGameUnit);
                db.SaveChanges();
            }

            this.GridViewAllUnitTypes.DataBind();
            this.CancelButton_Command(sender, e);
        }

    }
}